#include "StdAfx.h"
#include "dlg_taskbar_onlyone.h"

dlg_taskbar_onlyone::dlg_taskbar_onlyone(void)
{
	this->pwnd_ = NULL;
	this->pfunc_ = NULL;
	HINSTANCE hInst = LoadLibrary(_T("User32.DLL")); 
	if(hInst)  
	{
		this->pfunc_ = (set_layered_window_attr)GetProcAddress(hInst, "SetLayeredWindowAttributes");  	
	}
	this->trans_value_ = 0;
	//cs = 50;
}

dlg_taskbar_onlyone::~dlg_taskbar_onlyone(void)
{
	if( this->pwnd_ )
	{
		delete this->pwnd_;
	}
}

BOOL dlg_taskbar_onlyone::OnInitDialog()
{
	CDialog::OnInitDialog();
	CSetDPI dpi;
	dpi.Attach(AfxFindResourceHandle(MAKEINTRESOURCE(IDD), RT_DIALOG),
		m_hWnd, IDD, 96.0);
	HICON icon=AfxGetApp()->LoadIcon (IDR_MAINFRAME);
	SetIcon(icon, TRUE);
	SetIcon(icon, FALSE);
	GetDlgItem(IDC_BTN_CLOSE)->ShowWindow(SW_HIDE);
	dsSkinWindow(GetSafeHwnd(), SKIN_TYPE_DIALOG, "common.taskbar.dlg", TRUE);
	dsSkinWindow(GetDlgItem(IDC_BTN_UP)->GetSafeHwnd(), SKIN_TYPE_BUTTON, "common.taskbar.page_up", FALSE);
	dsSkinWindow(GetDlgItem(IDC_BTN_DOWN)->GetSafeHwnd(), SKIN_TYPE_BUTTON, "common.taskbar.page_down", FALSE);
	dsSkinWindow(GetDlgItem(IDC_BTN_CLOSE)->GetSafeHwnd(), SKIN_TYPE_BUTTON, "btnSysClose", FALSE);
	SetWindowText(this->title_);
	begin_display();
	return TRUE;
}

void dlg_taskbar_onlyone::OnTimer(UINT_PTR nIDEvent)
{
	switch( nIDEvent )
	{
	case 1:
		{
			if( this->pfunc_ )  
			{   
				//TRACE("1 %d", this->trans_value_);
				if( this->trans_value_ == 5 )
				{
					//TRACE("11 %d", this->trans_value_);
					ShowWindow(SW_SHOWNOACTIVATE);
				}
				if( this->trans_value_ >=0 && this->trans_value_ <= 255 )
				{
					this->pfunc_(GetSafeHwnd(), RGB(0,0,0), this->trans_value_, 2);
					if( this->trans_value_ <= 250 )
					{
						this->trans_value_ += 5;
					}
				}
				
				if( this->trans_value_ >= 255 )
				{
					//TRACE("12 %d", this->trans_value_);
					KillTimer(1);
					SetTimer(2, DISPLAY_TIME, NULL);
				}
				
			}
			break;
		}
	case 2:
		{
			TRACE("2 %d", this->trans_value_);
			KillTimer(2);
			if( is_disappeared_ )
			{
				SetTimer(3, MOVETIME_BETWEEN, NULL);
			}
			break;
		}
	case 3:
		{
			if( this->pfunc_ )
			{
				//TRACE("3 %d", this->trans_value_);
				if( this->trans_value_ >=0 && this->trans_value_ <= 255 )
				{
					this->pfunc_(GetSafeHwnd(), RGB(0,0,0), this->trans_value_, 2);
					if( this->trans_value_ >= 5 )
					{
						this->trans_value_ -= 5;
					}
				}
				
				if( this->trans_value_ < 30 )
				{
					//TRACE("31 %d", this->trans_value_);
					KillTimer(3);
					this->is_close_ = TRUE;
				}
			}
			break;
		}
	}
	CDialog::OnTimer(nIDEvent);
	if( this->is_close_ )
	{
		OnClose();
	}
}

BOOL dlg_taskbar_onlyone::insert_page(CWnd* child_wnd, CString id)
{
	if( this->pwnd_ != NULL )
	{
		this->pwnd_->DestroyWindow();
		delete this->pwnd_;
	}
	create_child_dlg(child_wnd);
	this->pwnd_ = child_wnd;
	TRACE("000");
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);
	SetTimer(1, MOVETIME_BETWEEN , NULL);
	SetWindowPos(CWnd::FromHandle(HWND_TOPMOST),0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE); 
	return TRUE;
}

BOOL dlg_taskbar_onlyone::delete_page(int i)
{
	return TRUE;
}

CWnd* dlg_taskbar_onlyone::find_page(CString id)
{
	return this->pwnd_;
}

LRESULT
dlg_taskbar_onlyone::on_taskbar_del_page(WPARAM wParam, LPARAM lParam)
{
	int i = (int)wParam;
	delete_page(i);
	return 0;
}

void dlg_taskbar_onlyone::init_taskdlg(const char* title, int width, int height, int type /* = aot::common_dlg::etaskdlg_unknown */)
{
	this->width_ = width;
	this->height_ = height;
	this->task_type_ = type;
	this->title_ = title;
}

BOOL dlg_taskbar_onlyone::begin_display()
{
	HWND hwnd;
	int taskbar_height = 0;
	hwnd = ::FindWindow("Shell_TrayWnd", NULL);
	CWnd *pwnd = CWnd::FromHandle(hwnd);
	if(pwnd != NULL)
	{
		CRect task_rect;
		pwnd->GetWindowRect(task_rect);
		taskbar_height = task_rect.Height();
	}
	int xscreen = GetSystemMetrics(SM_CXSCREEN);
	int yscreen = GetSystemMetrics(SM_CYSCREEN);
	CRect r, rchild;
	r.left = xscreen - this->width_;
	r.top = yscreen - this->height_ - taskbar_height;
	r.right = xscreen;
	r.bottom = yscreen - taskbar_height;
	MoveWindow(r);
	//ShowWindow(SW_HIDE);
	//SetWindowPos(CWnd::FromHandle(HWND_TOPMOST), r.left, r.top, this->width_, this->height_, SWP_NOACTIVATE);
	//SetWindowPos(CWnd::FromHandle(HWND_TOPMOST),0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); 
	this->end_top_point_ = r.top - taskbar_height;
	this->move_between_ = this->height_ / MOVETIMES;
	GetDlgItem(IDC_BTN_UP)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_PAGE_INFO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_DOWN)->ShowWindow(SW_HIDE);
	SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | 0x80000L);
	SetTimer(1, MOVETIME_BETWEEN , NULL);
	return TRUE;
}